import sys
import os
from pathlib import Path
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QSettings,QStandardPaths,QDir,QFile
from PySide6.QtGui import QIcon
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtQuickControls2 import QQuickStyle
#import Bridge
import BppTable_rc
import bpptablecolumn
import bpptablecolumnlist
import bpptableheading
import bpptablemodel
import platform
import logging
from PySide6.QtSql import QSqlDatabase
from logging.handlers import RotatingFileHandler
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
sys.stderr = open(sys.stderr.fileno(), mode='w', encoding='utf8', buffering=1)
handlers=[RotatingFileHandler(filename="report.log", maxBytes=10*1024*1024, encoding='utf-8',backupCount=3),logging.StreamHandler(sys.stdout)]
logging.basicConfig(level=logging.DEBUG,
                    format = '%(asctime)s - %(name)s - %(levelname)s -%(funcName)s - %(message)s',
                    handlers=handlers)
logging.info('begin')
def connectToDatabase():
    database = QSqlDatabase.database();
    if (not database.isValid()):
        database = QSqlDatabase.addDatabase("QSQLITE");
        if (not database.isValid()):
            logging.info("Cannot add database: %s", str(database.lastError().text()));

    writeDir = QDir(QStandardPaths.writableLocation(QStandardPaths.AppDataLocation))
    logging.info(writeDir)
    if (not writeDir.mkpath(".")):
        logging.info("Failed to create writable directory at %s", str(writeDir.absolutePath()));

    # // Ensure that we have a writable location on all devices.
    fileName = writeDir.absolutePath() + "/chat-database.sqlite3";
    # // When using the SQLite driver, open() will create the SQLite database if it doesn't exist.
    database.setDatabaseName(fileName);
    if (not database.open()):
        logging.info("Cannot open database: %s", str(database.lastError().text()));
        QFile.remove(fileName);
def main():
    # if platform.uname().system=="Windows":
    #     pass
    # else:
    #     from Watcher import Watcher
    #     Watcher()
    QApplication.setApplicationName("report")
    # QApplication.setOrganizationName("QtProject")

    app = QApplication(sys.argv)
    # connectToDatabase()
    # print(dir(app))
    # QIcon.setThemeName("gallery")

    # settings = QSettings()
    # if not os.environ.get("QT_QUICK_CONTROLS_STYLE"):
    #     style_name = settings.value("style")
    #     if style_name:
    #         QQuickStyle.setStyle(style_name)
    # QQuickStyle.setStyle("Material")
    engine = QQmlApplicationEngine()
    # built_in_styles = ["Basic", "Fusion", "Imagine", "Material", "Universal"]
    # if platform.system() == "Darwin":
    #     built_in_styles.append("macOS")
    # elif platform.system() == "Windows":
    #     built_in_styles.append("Windows")

    # Get the path of the current directory, and then add the name
    # of the QML file, to load it.
    qml_file = Path(__file__).parent / 'main.qml'
    # qml_file = Path(__file__).parent / 'C1.qml'
    engine.load(qml_file)

    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec())
if __name__ == '__main__':
    main()
    
